package com.ztf.room.controller;

import com.github.pagehelper.PageInfo;
import com.ztf.room.bean.*;
import com.ztf.room.service.ActivityService;
import com.ztf.room.utils.Result;
import com.ztf.room.utils.ResultInfo;
import com.ztf.room.utils.SessionConstant;
import com.ztf.room.utils.TimeUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/activity")
public class ActivityController {

    @Autowired
    private ActivityService activityService;

    // 活动列表
    @RequestMapping("/activityApply")
    public String activityApply(){
        return "student/activity.html";
    }

    // 获取活动的记录
    @RequestMapping("/getActivityList")
    @ResponseBody
    public Result getActivityList(int page, int limit, HttpSession session){
        Student student = (Student) session.getAttribute(SessionConstant.STUDENT_NAME);
        student.setStudentId(1);
        PageInfo<ActApplys> pageInfo =  activityService.getActivityList(student.getStudentId(),page,limit);
        return ResultInfo.success(pageInfo);
    }

    // 审核页面
    @RequestMapping("/auditActivity")
    public String auditActivity(){
        return "student/auditActivity.html";
    }

    // 管理员进行活动审核
    @RequestMapping("/saveApplyActivity")
    @ResponseBody
    public Result saveApplyActivity(ActApplys actApplys, HttpSession session){
        Teachers teachers = (Teachers) session.getAttribute(SessionConstant.TEACHER_NAME);
        activityService.saveApplyActivity(actApplys,teachers);
        return ResultInfo.success();
    }

    // 获取申请的活动
    @RequestMapping("/getApplyList")
    @ResponseBody
    public Result getApplyList(int page,int limit){
        PageInfo<ActApplys> pageInfo = activityService.getApplyList(page,limit);
        return ResultInfo.success(pageInfo);
    }

    // 申请活动表单页面
    @RequestMapping("/addActivity")
    public String addActivity(){
        return "student/page/addActivity.html";
    }

    // 提交活动申请
    @RequestMapping("/saveActivityApply")
    @ResponseBody
    public Result saveActivityApply(ActApplys actApplys,String activityNote, HttpSession session){
        Student student = (Student) session.getAttribute(SessionConstant.STUDENT_NAME);
        Activities activities = new Activities();
        activities.setActivityNote(activityNote);

        actApplys.setStudentId(student.getStudentId())
                .setStudentName(student.getStudentName())
                .setStudentAcademy(student.getStudentAcademy())
                .setApplyDate(TimeUtils.nowTime());

        BeanUtils.copyProperties(actApplys,activities);
        activityService.addActivityApply(actApplys,activities);

        return ResultInfo.success();
    }


    // 活动申请结果
    @RequestMapping("/activityApplyResult")
    public String activityApplyResult(){

        return "student/activityResult.html";
    }

    // 获取活动申请结果列表
    @RequestMapping("/getActivityResult")
    @ResponseBody
    public Result getActivityResult(){
        ActApplyResults results = new ActApplyResults();
        results.setActApplyResultId(1)
                .setActApplyId(1)
                .setApplyresultsDate("2021-1-1")
                .setTeacherDepart("基础部")
                .setTeacherId(1)
                .setTeacherName("ztf")
                .setApproveOrnot("1");
        Object[] a = {results};
        return ResultInfo.success(a);
    }

}
